/*
 *    Copyright (c) 2018-2025, envpro All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: envpro
 */

package com.iwomy.secureplat.platform;


import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.iwomy.secureplat.platform.constant.RedisConstants;
import com.iwomy.secureplat.platform.entity.BasicDeviceEntity;
import com.iwomy.secureplat.platform.service.BasicDeviceService;
import com.iwomy.secureplat.platform.utils.RedisClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.Set;

@Component
@Slf4j
public class SecureplatPlatformInitialization {

	@Resource
	private BasicDeviceService basicDeviceService;
	@Resource
	private RedisClient redisClient;

	@PostConstruct
	private void init() {
		//项目刚启动的时候初始化所有设备为离线状态
//		List<Integer> list = new ArrayList(Arrays.asList(20, 30, 40));
		LambdaUpdateWrapper<BasicDeviceEntity> updateWrapper = Wrappers.lambdaUpdate(BasicDeviceEntity.class)
				.set(BasicDeviceEntity::getOnline, 10).eq(BasicDeviceEntity::getOnline, 20);
		basicDeviceService.update(updateWrapper);
		log.info("初始化项目,设备全部离线");

		Set<String> strings = redisClient.listKeyByPrefix(RedisConstants.REDIS_KEY_DEVICE_ONLINE);
		if (ObjectUtil.isEmpty(strings)) return;
		strings.stream().forEach(redisClient::del);


	}

}
